<template>
  <div style="background-color: orange">
    <div class="create">
      <div class="head">
        <h2>欢迎发布新菜谱，先介绍一下你的大作！</h2>
        <section class="create-introduce">
          <h5>标题</h5>

          <el-input
            class="create-input"
            placeholder="请输入内容"
            v-model="addData.Title"
          ></el-input>
          <h5>属性</h5>
          <div>
            <el-select v-model="addData.ScreenId">
              <el-option
                v-for="(item, index) in bindscreen"
                :key="index"
                :value="item.ScreenId"
                >{{ item.ScreenName }}</el-option
              >
            </el-select>
          </div>
          <h5>菜谱分类</h5>
          <div>
            <el-select placeholder="请选择菜谱分类">
              <el-option-group>
                <el-option> </el-option>
              </el-option-group>
            </el-select>
          </div>
          <h5>成品图 (328*440)</h5>
          <div class="upload-img-box clearfix">
            <div class="upload-img">
              <upload-img>
                <el-upload
                  class="avatar-uploader"
                  action="http://localhost:55943/api/UpLoad"
                  :show-file-list="false"
                  :on-success="handleAvatarSuccess"
                  :before-upload="beforeAvatarUpload"
                >
                  <img v-if="imageUrl" :src="imageUrl" class="avatar" />
                  <i v-else class="el-icon-plus avatar-uploader-icon"></i>
                </el-upload>
              </upload-img>
            </div>
          </div>
        </section>
      </div>
      <h2>记录所有原材料</h2>
      <div class="zhong">
        <section class="create-introduce">
          <div>
            <h5>主料</h5>
            <el-col :span="5">
              <el-input
                v-model="addData.CLngredients"
                placeholder="请输入内容"
              ></el-input>
              <el-button
                class="kk"
                type="primary"
                size="medium"
                icon="el-icon-plus"
                >增加一</el-button
              >
            </el-col>
          </div>
          <!--[ { "name": "", "specs": "" }, { "name": "", "specs": "" }, { "name": "", "specs": "" } ]-->

          <div class="kk1">
            <h5>辅料</h5>
            <el-col :span="5">
              <el-input
                v-model="addData.CSupplementary"
                placeholder="请输入内容"
              ></el-input>
              <el-button
                class="kk"
                type="primary"
                size="medium"
                icon="el-icon-plus"
                >增一步</el-button
              >
            </el-col>
          </div>
        </section>
      </div>

      <h2>开始写步骤了！能否简单易学就看你怎么写了，加油！</h2>
      <div class="xia">
        <section class="create-introduce">
          <div>
            <el-input
              class="introduce-text"
              type="textarea"
              :rows="2"
              placeholder="请输入内容"
              v-model="addData.Step"
            >
            </el-input>
            <el-button
              class="eaeaea add-step-button"
              type="primary"
              size="medium"
              icon="el-icon-plus"
              >增加一步</el-button
            >
          </div>
          <div>
            <h5>烹饪小技巧</h5>
            <el-input
              class="introduce-text"
              type="textarea"
              :rows="8"
              placeholder="分享下你做这道菜的过程中的心得和小技巧吧！"
              v-model="addData.CCokking"
            >
            </el-input>
          </div>
        </section>
      </div>
      <el-button
        class="send"
        type="primary"
        size="medium"
        :icon="icon"
        @click="add"
        >搞定，提交审核</el-button
      >
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      addData: {
        CId: "",
        CName: "",
        CImg: "",
        CNum: "",
        CWriter: "",
        TypeId: "",
        TypeCode: "",
        ScreenID: "",
        Title: "",
        CLngredients: "",
        CSupplementary: "",
        Step: "",
        CCokking: "",
        Status: "",
      },
      bindscreen: [],
      imageUrl:''
    };
  },
  methods: {
    add() {
      this.addData.CName = this.addData.Title;
      this.$axios
        .post("http://localhost:55943/api/AddFood", this.addData)
        .then((res) => {
          if (res.data > 0) {
            this.$message.success("发布成功");
          } else {
            this.$message.error("添加失败");
          }
        });
    },
    bindScreen() {
      this.$axios.get("http://localhost:55943/api/BindScreen").then((res) => {
        this.bindscreen = res.data;
      });
    },
    handleAvatarSuccess(res, file) {
        this.addData.CImg=res.Url;
        this.imageUrl = URL.createObjectURL(file.raw);
      },
      beforeAvatarUpload(file) {
        const isJPG = file.type === 'image/jpeg';
        const isLt2M = file.size / 1024 / 1024 < 2;

        if (!isJPG) {
          this.$message.error('上传头像图片只能是 JPG 格式!');
        }
        if (!isLt2M) {
          this.$message.error('上传头像图片大小不能超过 2MB!');
        }
        return isJPG && isLt2M;
      }
  },
  created() {
    this.bindScreen();
  },
};
</script>

<style scope>
.head{
    height: 700px;
    background-color: #fff;
}
.avatar-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
  }
  .avatar-uploader .el-upload:hover {
    border-color: #409EFF;
  }
  .avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 178px;
    height: 178px;
    line-height: 178px;
    text-align: center;
  }
  .avatar {
    width: 178px;
    height: 178px;
    display: block;
  }
.create-introduce {
  background-color: #fff;
  padding: 20px;
}

.add-step-button {
  margin-left: -650px;
  margin-top: 80px;
}

.create {
  width: 80%;
  margin: 0 auto;
  text-align: left;
}

h2 {
  text-align: center;
  margin: 20px 0;
}

.send {
  height: 70px;
  width: 220px;
  background: #ff3232;
  color: #fff;
  border: none;
  margin: 20px auto;
  display: block;
}

h5 {
  margin-top: 20px;
  margin-left: 20px;
}

.create-input input {
  width: 446px;
  line-height: 22px;
}

.upload-img-box {
  float: left;
}
.upload-img {
  float: left;
}

.introduce-text {
  width: 100px;
  float: left;
  height: -200px;
}

.el-textarea {
  width: 60%;
  margin-left: 10px;
}
.xia {
  width: 100%;
  height: 400px;
  background-color: #fff;
}
.zhong {
  width: 100%;
  height: 350px;
  background-color: #fff;
}
.kk1 {
  margin-top: 120px;
}
</style>